import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/*
 * Servico do Observador
 */

public class RemoteServiceObserver extends UnicastRemoteObject implements
		RemoteServiceObserverInterface {
	public RemoteServiceObserver() throws RemoteException {
	}

	public void notifyNewOperationConcluded(String description)
			throws RemoteException {
		System.out.println(description);
	}

	public static void main(String[] args) {
		try {

			// Cria e lanca o servico
			RemoteServiceObserver observer = new RemoteServiceObserver();
			System.out
					.println("Servico GetRemoteFileObserver criado e em execucao...");

			// Localiza o servico remoto nomeado "GetRemoteFile"
			String objectUrl = "rmi://127.0.0.1/RemoteService"; // rmiregistry
																// on localhost

			if (args.length > 0)
				objectUrl = "rmi://" + args[0] + "/RemoteService";

			RemoteServiceInterface RemoteService = (RemoteServiceInterface) Naming
					.lookup(objectUrl);

			// adiciona observador no servico remoto
			RemoteService.addObserver(observer);

			System.out.println("<Enter> para terminar...");
			System.out.println();
			System.in.read();

			RemoteService.removeObserver(observer);
			UnicastRemoteObject.unexportObject(observer, true);

		} catch (RemoteException e) {
			System.out.println("Erro remoto - " + e);
			System.exit(1);
		} catch (Exception e) {
			System.out.println("Erro - " + e);
			System.exit(1);
		}
	}
}
